package org.herui.campus.service.auth;


import org.apache.commons.lang3.StringUtils;
import org.shan.security.core.authentication.userinfo.UserInfoProcess;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.userdetails.User;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by amanda.shan on 2019/7/1.
 */
@Service
public class AddUserNameProcess implements UserInfoProcess {

    @Override
    public void process(Authentication authentication, HttpServletRequest httpServletRequest,
                        HttpServletResponse httpServletResponse) {

        String username = null;
        Object principal = authentication.getPrincipal();
        if (principal instanceof User) {
            User user = (User) principal;
            username = user.getUsername();
        } else {
            username = principal.toString();
        }

        if (!StringUtils.isEmpty(username)) {
            httpServletRequest.setAttribute("username", username);
        }
    }



}
